package com.taotao.manage.controller;

import com.fasterxml.jackson.databind.ObjectMapper;

import com.taotao.common.utils.ItemCatResult;

import com.taotao.manage.pojo.ContentCategory;
import com.taotao.manage.pojo.ItemCat;
import com.taotao.manage.service.ContentCategoryService;
import com.taotao.manage.service.ItemCatService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


/**
 * 内容（广告）分类
 * @author 铭
 *
 */
@RequestMapping("content/category")
@Controller
public class ContentCategoryController {
    private static final ObjectMapper MAPPER = new ObjectMapper();
    private static final Logger LOGGER = LoggerFactory.getLogger(ContentCategoryController.class);
    @Autowired
    private ContentCategoryService categoryService;

    /**
     * 查询内容树状结构
     * @param parentId
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<List<ContentCategory>> queryContentCategoryList(
        @RequestParam(value = "id", defaultValue = "0")
    Long parentId) {
        try {
            ContentCategory contentCategory = new ContentCategory();
            contentCategory.setParentId(parentId);

            List<ContentCategory> list = this.categoryService.queryByWhere(contentCategory);

            if ((list == null) || (list.size() == 0)) {
                //资源不存在，返回404
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
            }

            return ResponseEntity.ok(list);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                                 .body(null);
        }
    }

    /**
     * 新增内容  子节点
     * @param parentId
     * @return
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ContentCategory> saveContentCategory(
        ContentCategory contentCategory) {
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("新增内容！ contentCategory={}", contentCategory);
            }

            Integer count = categoryService.saveContentCategory(contentCategory);

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("新增内容成功！ contentCategory={}", contentCategory);
            }

            //新增成功，返回201
            return ResponseEntity.status(HttpStatus.CREATED).body(contentCategory);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("500 新增内容失败！ contentCategory={}", contentCategory);
            }

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
        }
    }

    /**
     * 更改内容名字
     * @param parentId
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<Void> updateContentCategoryName(
        @RequestParam("id")
    Long id, @RequestParam("name")
    String name) {
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("更改内容名字！ id = {},name={}", id, name);
            }

            ContentCategory contentCategory = new ContentCategory();
            contentCategory.setId(id);
            contentCategory.setName(name);

            Integer count = categoryService.updateSelective(contentCategory);

            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("更改名字内容成功！id = {},name={}", id, name);
            }

            //更名成功，返回200
            return ResponseEntity.status(HttpStatus.OK).build();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("500 更名失败！id = {},name={}", id, name);
            }

            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                                 .build();
        }
    }

    /**
    * 删除内容节点以及所有的子节点
    *
    * @param contentCategory
    * @return
    */
    @RequestMapping(method = RequestMethod.DELETE)
    public ResponseEntity<Void> delete(ContentCategory contentCategory) {
        try {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("删除内容节点！ contentCategory={}", contentCategory);
            }

            categoryService.deleteContentCategory(contentCategory);

            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } catch (Exception e) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("删除内容节点失败！ contentCategory={}", contentCategory, e);
            }
        }

        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
